<?php

class Util {
	
	private static $encryptType = "SHA256";
	private static $encryptKey  = BASE_PROJECT_PATH;
	
	public static function forward($path) {
		//header("Location:$path");	 
		echo "<script Language=\"JavaScript\">location.href = ('$path');</script>";
	}
	
	public static function clientIp(){
	    if (!empty($_SERVER['HTTP_CLIENT_IP'])) { // check ip from share internet
	        $ip = $_SERVER['HTTP_CLIENT_IP'];
	    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { // to check ip is pass from proxy
	        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	    } else {
	        $ip = $_SERVER['REMOTE_ADDR'];
	    }
	    return $ip;
	}
	
	public static function alert($str, $code = "utf-8") {
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=$code\">";
		echo "<script language=\"JavaScript\">";
		echo "alert('$str');";
		echo "</script>";
	}
	
	/**
	 * to echo for app
	 */
	public static function toEcho($str){
		ob_clean();
		$str = str_replace("\r\n", "", $str);
		echo $str;
	}
	
	/**
	 * to echo json for app
	 */
	public static function toEchoJson($arr){
		$str = json_encode ( $arr );
		$str = str_replace("\r\n", "", $str);
		ob_clean();
		echo $str;
	}
	
	/**
	 * Object Array to JSON String
	 * Enter description here ...
	 * @param unknown_type $arr
	 */
	public static function toJson($arr) {
		return json_encode ( $arr );
	}
	
	/**
	 * JSON String to Array
	 * Enter description here ...
	 * @param unknown_type $json
	 */
	public static function jsonToArray($json) {
		return json_decode ( $json, true );
	}
	
	/**
	 * JSON String to Object
	 * @param unknown_type $json
	 */
	public static function jsonToObject($json) {
		return json_decode ( $json );
	}
	
	/**
	 * Get now date
	 * Enter description here ...
	 */
	public static function getNowDate($style = "Y/m/d H:i:s") {
		date_default_timezone_set ( 'Asia/Taipei' );
		return date ( $style );
	}
	
	public static function encrypt($input_string){
		return hash(Util::$encryptType, $input_string.Util::$encryptKey);
	}
	
	/**
	 * get device key
	 */
	public static function getDeviceKey($string_length){
		$string = "";
		// remove o,0,l,!@#$%^&*()-=,<>;{}[]ABCDEFGHIJKLMNPQRSTUVWXYZ
		//$word = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
		$word = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
		$len = strlen($word);
		for ($i = 0; $i < $string_length; $i++) {$string .= $word[rand() % $len];}
		return $string;
	}

}

?>